package com.qingcheng.componet;

import com.qingcheng.pojo.goods.Sku;
import org.apache.http.HttpHost;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

@Component
public class RabbitMqConsumer {

    /**
     * queuesToDeclare 表示为创建一个队列
     * 删除操作有两种方式，一种是通过id来进行删除，但是这种方式一般不常用，因为id不容易得到
     * @param id
     * @throws IOException
     */
    @RabbitListener(queuesToDeclare = @Queue("queue.sku_del"))
    public void isDelete(String id) throws IOException {
        // 连接elasticsearch
        HttpHost http = new HttpHost("127.0.0.1", 9200, "http");
        RestClientBuilder builder = RestClient.builder(http);
        RestHighLevelClient restHighLevelClient = new RestHighLevelClient(builder);

        // 删除指定的索引值
        DeleteRequest deleteRequest = new DeleteRequest("my_sku15", "doc", id);
        DeleteResponse delete = restHighLevelClient.delete(deleteRequest, RequestOptions.DEFAULT);
        System.out.println(delete);

        restHighLevelClient.close();
    }

    @RabbitListener(queuesToDeclare = @Queue("queue.sku_add"))
    public void isAdd(Sku sku) throws IOException {
        // 连接elasticsearch服务端
        HttpHost http = new HttpHost("127.0.0.1", 9200, "http");
        RestClientBuilder builder = RestClient.builder(http);
        RestHighLevelClient restHighLevelClient = new RestHighLevelClient(builder);

        IndexRequest indexRequest = new IndexRequest("my_sku15", "doc", sku.getId());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String format = sdf.format(sku.getCreateTime());

        Map map = new HashMap();
        map.put("name", sku.getName());
        map.put("brandName", sku.getBrandName());
        map.put("price", sku.getPrice());
        map.put("createTime", format);
        map.put("saleNum", sku.getSaleNum());
        map.put("commentNum", sku.getCommentNum());
        String specsize = sku.getSpec().substring(sku.getSpec().lastIndexOf("{") + 1, sku.getSpec().length());
        String substring = specsize.substring(0, specsize.indexOf("}"));
        String[] split = substring.split(",");
        Map spec = new HashMap();
        int k=0;
        for (String i:split
             ) {
            String m=  i.replaceAll("\'","");
            String[] split1 = m.split(":");
            for (k=0;k<split1.length;k++){
                spec.put(split1[k],split1[k+1]);
                k+=k+2;
            }
        }
        map.put("spec",spec);
        indexRequest.source(map);
        //响应结果
        IndexResponse index = restHighLevelClient.index(indexRequest,RequestOptions.DEFAULT);
        int status=index.status().getStatus();
        System.out.println(status);
        restHighLevelClient.close();

    }

}
